package com.lanren.familybookapp.app;

import com.lanren.common.app.ApiUrl;
import com.lanren.common.utils.md5.MD5Utils;
import com.lanren.easyhttp.interceptor.BaseDynamicInterceptor;
import com.lanren.easyhttp.utils.HttpLog;

import java.util.Map;
import java.util.TreeMap;

/**
 * @ClassName CustomSignInterceptor
 * @Description TODO 衔接参数过滤器
 * @Author chongheng.wang
 * @Date 2019/11/6 17:09
 * @Version 1.0
 */
public class CustomSignInterceptor extends BaseDynamicInterceptor<CustomSignInterceptor> {
    @Override
    public TreeMap<String, String> dynamic(TreeMap<String, String> dynamicMap) {
        //dynamicMap:是原有的全局参数+局部参数
        //你不必关心当前是get/post/上传文件/混合上传等，库中会自动帮你处理。
        //根据需要自己处理，如果你只用到token则不必处理isTimeStamp()、isSign()

        //Logc.i("dynamicMap:" + dynamicMap.toString());
        return dynamicMap;//dynamicMap:是原有的全局参数+局部参数+新增的动态参数
    }

    //示例->签名规则：POST+url+参数的拼装+secret
    private String sign(TreeMap<String, String> dynamicMap) {
        String url = getHttpUrl().url().toString();
        url = url.replaceAll("%2F", "/");
        StringBuilder sb = new StringBuilder("POST");
        sb.append(url);
        for (Map.Entry<String, String> entry : dynamicMap.entrySet()) {
            sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }
        sb.append(ApiUrl.HEADER.getAppSecret());
        HttpLog.i(sb.toString());
        return MD5Utils.MD5Encode32(sb.toString());
    }

}
